import pymysql

from conn_mysql.conn_mysql import ConnectMysql

import datetime

class ApiUpdate(ConnectMysql):

    def open_id(self,openid,company_id):
        '''Look up all the information in the openid table,
        parameter: the user corresponds to openid'''

        sql = 'select * from openid where openid=%s and company_id=%s'

        result_info = self.select_one(sql, (openid, company_id))

        return result_info

    def company_info(self, company_id):
        '''Present the data in the company table'''

        sql = 'select * from company where id = %s'

        result_info = self.select_one(sql, (company_id))

        return result_info

    def need_his_register(self, company_id, need):
        
        sql = '''INSERT INTO need_his(company_id, need) values (%s,%s);'''
        
        result_info = self.insert_info(sql, (company_id, need))

        return result_info

    def update_need(self, need, party, company_id):
        '''update_need'''
    
        sql = '''UPDATE company SET need=%s, party=%s WHERE id=%s'''

        result_info = self.update_info(sql, (need, party, company_id))

        return result_info

    def update_company_need(self, methods, company_id, need, party, openid):

        if not methods == 'PUT':
        
            return {
                'status': -1,
                'errorCode': 4,
                'errorMsg':f'请检查request.method:{methods}请重新请求',
                'resultBody':None
            }

        if not all((company_id, need, party, openid)):
    
            return {
                'status': -1,
                'errorCode': 3,
                'errorMsg':f'请检查参数need:{need},company_id:{company_id},openid:{openid},party:{party}是否为空请重新请求',
                'resultBody':None
            }

        else:

            if not party in ('A','B'):

                return {
                    'status': -1,
                    'errorCode': 7,
                    'errorMsg':f'请检查参数party:{party}是否为A or B请重新请求',
                    'resultBody':None
                }

            openid_table_info = self.open_id(openid, company_id)

            if not openid_table_info:
        
                return {
                    'status': -1,
                    'errorCode': 6,
                    'errorMsg':f'请检查参数是否存在,openid:{openid},请重新输入',
                    'resultBody':None
                }

            else:

                company_update = self.company_info(company_id)['update_time']

                now = datetime.datetime.now()

                time_line = datetime.timedelta(seconds=3600*(24-8))

                if now-company_update > time_line:

                    update_work = self.update_need(need, party, company_id)

                    if update_work:

                        if self.need_his_register(company_id, need):

                            return {
                                    'status': 1,
                                    'errorCode': None,
                                    'errorMsg':None,
                                    'resultBody':'更改 new need 成功'
                                }

                        else:

                            return {
                                    'status': -1,
                                    'errorCode': 2,
                                    'errorMsg':'mysql unknown error',
                                    'resultBody':None
                                }

                    else:

                        return {
                            'status': -1,
                            'errorCode': 2,
                            'errorMsg':f'请检查参数company_id:{company_id}是否正确,请重新请求',
                            'resultBody':None
                        }

                else:

                    return {
                        'status': -1,
                        'errorCode': 5,
                        'errorMsg':'今天次数更改已用完',
                        'resultBody':None
                    }
